<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\model\api;

use laytp\BaseModel;
use think\model\concern\SoftDelete;

class YysHero extends BaseModel
{
    protected $name = 'yys_hero';


    /**
     * 角色列表
     * @param $keyword
     * @param $category
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function list($keyword,$category,$limit = 10){
        $where = [];
        if($keyword){
            $where[] = ['name', 'like', '%'.$keyword.'%'];
        }
        if($category){
            $where[] = ['category', '=', $category];
        }

        $list = $this->where($where)
            ->field('id,name,icon as headimg,quality,position')
            ->order('sort desc')
            ->cache(true)
            ->paginate($limit)
            ->toArray();

        return $list;
    }

    /**
     * 角色详情
     * @param $role_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail($role_id){
        $detail = $this->where('id',$role_id)
            ->cache(true)->find();

        $skill = (new YysSkill())->detail($role_id);
        $return = array(
            'id'   => $detail['id'],
            'name' => $detail['name'],
            'quality'  => $detail['quality'],
            'position' => $detail['position'],
            'img' => $detail['img'],
            'start_attribute' => json_decode($detail['start_attribute'],true),
            'end_attribute'   => json_decode($detail['end_attribute'],true),
            'start_ability'   => explode('-',$detail['start_ability']),
            'end_ability'     => explode('-',$detail['end_ability']),
            'skill'       => !empty($skill['skill'])?$skill['skill']:[],
            'skill_grade' => !empty($skill['skill_grade'])?$skill['skill_grade']:[],
            'jx'   => (new YysJx())->detail($role_id),
            'yh'   => (new YysYh())->detail($role_id),
            'pvp'  => json_decode($detail['pvp'],true),
            'pve'  => json_decode($detail['pve'],true),
            'desc' => !empty($detail['desc'])?$detail['desc']:'',
        );
        return $return;
    }
}